from src.common.db.database import DatabaseManager
from src.repository.base_repository import BaseRepository
from src.service.base_service import BaseService
from loguru import logger

class SimpleContainer:
    """简化的容器类，不使用dependency-injector"""
    
    def __init__(self):
        self.config = {}
        self._database_manager = None
    
    def get_database_manager(self):
        """获取数据库管理器实例"""
        if self._database_manager is None:
            self._database_manager = DatabaseManager()
        return self._database_manager
    
    def create_repository(self, repository_class, model_class=None):
        """创建Repository实例"""
        if model_class:
            return repository_class(model_class)
        return repository_class()
    
    def create_service(self, service_class, repository=None):
        """创建Service实例"""
        if repository:
            return service_class(repository=repository)
        return service_class()

class ContainerManager:
    """简化的容器管理器"""
    
    def __init__(self):
        self.container = SimpleContainer()
    
    def init_container(self, config: dict = None):
        """初始化容器
        
        Args:
            config: 配置字典
        """
        if config:
            self.container.config.update(config)
        
        logger.info("简化容器初始化完成")
    
    def wire_modules(self, modules: list):
        """装配模块（简化版，实际不做任何操作）
        
        Args:
            modules: 需要装配的模块列表
        """
        logger.info(f"跳过模块装配（简化版）: {modules}")
    
    def get_container(self) -> SimpleContainer:
        """获取容器实例"""
        return self.container
    
    def shutdown(self):
        """关闭容器"""
        logger.info("简化容器已关闭")

# 全局容器管理器实例
container_manager = ContainerManager()

# 便捷函数
def get_container() -> SimpleContainer:
    """获取全局容器实例"""
    return container_manager.get_container()

def init_container(config: dict = None):
    """初始化全局容器"""
    container_manager.init_container(config)

def wire_modules(modules: list):
    """装配模块到全局容器（简化版）"""
    container_manager.wire_modules(modules)

def shutdown_container():
    """关闭全局容器"""
    container_manager.shutdown()